<?php
/**
 * @Author: liuhongbin
 * @Email: 514660094@qq.com
 * @Date: 2019-02-12 16:15:10
 * @Description:
 * @LastEditors: liuhongbin
 * @LastEditTime: xxx
 */

namespace app\adminapi\controller\v1\attachment;

use think\App;
use think\Request;
use hiduoke\services\FormBuilder as Form;
use app\common\controller\AdminController;
use app\common\model\attachment\AttachmentGroup as GroupModel;

class AttachmentGroup extends AdminController
{
    /**
     * @var GroupModel
     */
    public $model;

    public function __construct(App $app, GroupModel $model)
    {
        parent::__construct($app);
        $this->model = $model;
    }

    /**
     * 显示资源列表
     * @Route("AttachmentGroup", method="GET")
     * @return \think\Response
     */
    public function index()
    {
        // TODO 这里分组应该做部分限制
        $default_group = [
            ['name' => "全部", 'id' => "-1"],
            ['name' => "未分组", 'id' => "0"]
        ];
        $group = $this->model->where([
            'type'  => input('type', 1),
            'uid'   => input('uid', 0),
            'plaid' => input('plaid', 0)
        ])->order("sort desc")->select()->toArray();
        $group = array_merge($default_group, $group);
        return $this->success("ok", $group);
    }

    /**
     * 显示创建资源表单页.
     * @Route("AttachmentGroup/create", method="GET")
     * @return \think\Response
     */
    public function create()
    {
        $rule = array();
        $form = Form::createForm("/adminapi/attachment/group")->setMethod("POST");
        $rule[] = Form::input('name', '名称', "新分组")->required();
        $rule[] = Form::input('sort', '排序', "1");
        $rule[] = Form::hidden('type', "1");
        $form->setTitle("新增")->setRule($rule);
        return $this->success("", Form::getFormData($form));
    }

    /**
     * 保存新建的资源
     * @Route("AttachmentGroup", method="POST")
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $formData = $this->request->post("formData");
        $this->model->save($formData);
        return $this->success("新增成功");
    }

    /**
     * 显示指定的资源
     * @Route("AttachmentGroup/id", method="GET")
     * @param int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     * @Route("AttachmentGroup/id/edit", method="GET")
     * @param int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $rule = array();
        $form = Form::createForm("/adminapi/attachment/group/" . $id)->setMethod("put");
        $row = $this->model->where(['id' => $id])->findOrEmpty();
        $rule[] = Form::input('name', '名称', $row->name)->required();
        $rule[] = Form::input('sort', '排序', $row->sort);
        $form->setTitle("编辑")->setRule($rule);
        return $this->success("", Form::getFormData($form));
    }

    /**
     * 保存更新的资源
     * @Route("AttachmentGroup/id", method="PUT")
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $formData = $this->request->post("formData");
        $row = $this->model->where(['id' => $id])->find();
        if (!$row) {
            return $this->error("分组不存在");
        }
        $row->save($formData);
        return $this->success("编辑成功");
    }

    /**
     * 删除指定资源
     * @Route("AttachmentGroup/id", method="DELETE")
     * @param int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $this->model->where(['id' => $id])->delete();
        return $this->success("删除成功");
    }
}
